import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { Button, message } from 'antd';
import Modal from '../../../components/Modal/index';
import services from '../../../services/api';

class OuterList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'outerList';
    this.editButton = record => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.edit.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          编辑
        </Button>
      );
    };
    this.mapButton = record => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.map.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          映射
        </Button>
      );
    };
    this.delButton = record => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.delete.bind(this, record)}
          disabled={record.audit_status === this.AUDIT._WAIT}
        >
          删除
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.mapButton, this.delButton];
    this.state = {
      third_namelist_source: {}
    };
  }

  // 删除时的参数
  getDeleteParam = record => {
    return { third_list_id: record.third_list_id };
  };

  componentWillMount() {
    const third_namelist_source = {};
    this.props.third_namelist_source.forEach(item => {
      third_namelist_source[item.id] = item.value;
    });
    this.setState({
      third_namelist_source
    });
  }

  refreshTableFilter = params => {
    this.props.refreshTableFilter && this.props.refreshTableFilter(params);
  };

  delete(record) {
    if (record.strategy_count > 0) {
      message.error(`有${record.strategy_count}条策略使用了此三方名单，请先下线关联策略再删除此名单！`);
    } else {
      Modal.open({
        title: '',
        isConfirm: true,
        width: '300px',
        content: '确定要删除?',
        isOkBtn: true,
        isCancelBtn: true,
        ok: () => {
          services[this.api].delete(this.getDeleteParam(record)).then(data => {
            if (data.code === 0) {
              message.success(data.msg);
              this.props.queryList();
              Modal.close();
            } else {
              message.error(data.msg);
            }
          });
        },
        cancel: () => {
          Modal.close();
        }
      });
    }
  }
}

export default OuterList;
